package com.vgemv.jsutilitysdk.JsView.JsLogoView;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.vgemv.jsutilitysdk.geometry.JsGeometryManager;

public class JsLogoView extends LinearLayout {

    private ImageView mIv;
    private TextView mTv_title;
    private TextView mTv_detail;

    public String mStr_title;
    public String mStr_detail;

    public JsLogoView(Context context) {
        super(context);
        setOrientation(VERTICAL);
        setupUi();
        setBackgroundColor(Color.rgb(200,200,200));
    }

    void setupUi() {
        int padding = JsGeometryManager.pixelFromDp(30);
        Context context = this.getContext();
        {
            mIv = new ImageView(context);
            int size = JsGeometryManager.pixelFromDp(100);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(size,size);
            lp.gravity = Gravity.CENTER;
            lp.setMargins(0,padding,0,0);
            addView(mIv,lp);

            mIv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    rotate();
                }
            });
        }
        {
            mTv_title = new TextView(context);
            mTv_title.setTextSize(18);
            //android中为textview动态设置字体为粗体
            mTv_title.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            mTv_title.setTextAlignment(TEXT_ALIGNMENT_CENTER);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.setMargins(0,padding,0,0);
            addView(mTv_title,lp);
            
        }
        {
            mTv_detail = new TextView(context);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            mTv_detail.setTextAlignment(TEXT_ALIGNMENT_CENTER);
            lp.setMargins(0,padding,0,padding);
            addView(mTv_detail,lp);
        }
    }

    public void setTitle(String title) {
        mStr_title = title;
        mTv_title.setText(title);
    }
    public void setDetail(String detail) {
        mStr_detail = detail;
        mTv_detail.setText(detail);
    }

    public void setImageResource(int resId) {
        mIv.setImageResource(resId);
    }

    public void rotate() {
        // 下面是使用android自带的旋转动画
         RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
         rotateAnimation.setDuration(2000);
         mIv.startAnimation(rotateAnimation);
    }
}
